﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;

namespace LPV_POS.mh_pos
{
    public partial class bton_clr : DevExpress.XtraEditors.XtraForm
    {
        thuvien tv;
        DataTable tb;
        string[] s;
        object[] o;
        // select danh sach don vi tinh
        private DataTable f_selectdmHTTT()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmHTTT", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }
        // insert mot HTTT
        private int f_insertHTTT(string tenHTTT, Boolean hinhthuc)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@HINHTHUC" };
            o = new object[] { 2, 1, tenHTTT, hinhthuc};
            int ketqua = tv.SP_Update("sp_mh_dmHTTT", s, o);
            return ketqua;
        }

        // update HTTT
        private int f_updateHTTT(string maHTTT, string tenHTTT, Boolean hinhthuc)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@OBJNAME", "@HINHTHUC" };
            o = new object[] { 3, 1, maHTTT, tenHTTT, hinhthuc};
            int ketqua = tv.SP_Update("sp_mh_dmHTTT", s, o);
            return ketqua;
        }

        // xoa HTTT
        private int f_deletethueHTTT(string maHTTT)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, maHTTT };
            int ketqua = tv.SP_Update("sp_mh_dmHTTT", s, o);
            return ketqua;
        }
        // Tim VAT khi nhan nut tim
        private DataTable f_selectfindHTTT(string tenHTTT, int hinhthuc)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME", "@INT_HINHTHUC" };
            o = new object[] { 1, 2, tenHTTT, hinhthuc };
            tb = tv.SP_SelectDataTable("sp_mh_dmHTTT", s, o);
            return tb;
        }
        // tim HTTT
        private DataTable f_findHTTT()
        {
            DataTable tb;
            string tenHTTT;
            int hinhthuc=1;

            if (txt_tenhtthanhtoan.Text != "")
                tenHTTT = "'%" + txt_tenhtthanhtoan.Text + "%'";
            else
                tenHTTT = txt_tenhtthanhtoan.Text;

            if (com_hinhthucno.Text == "--- Chọn tất cả ---")
                hinhthuc = 1;
            else if (com_hinhthucno.Text == "Chọn")
                hinhthuc = 2;
            else
                hinhthuc = 3;


            tb = f_selectfindHTTT(tenHTTT, hinhthuc);
            return tb;

        }
        public bton_clr()
        {
            InitializeComponent();
        }

        private void mh_dmHTthanhtoan_Load(object sender, EventArgs e)
        {
            grid_hinhthucthanhtoan.OptionsBehavior.Editable = false;
            luoi_hinhthucthanhtoan.MainView = grid_hinhthucthanhtoan;
            col_hinhthuc.FieldName = "HINHTHUC";
            luoi_hinhthucthanhtoan.DataSource = f_selectdmHTTT();
            //bton_tim.Text = "CLR";

            bton_luu.Enabled = false;
            bton_tenhtthanhtoan.Enabled = false;
            com_hinhthucno.Visible = false;
         
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            txt_tenhtthanhtoan.Text = "";
            check_ththanhtoan.Checked = false;
            bton_tenhtthanhtoan.Enabled = true;
            bton_luu.Enabled = true;

            Variable.str_trangthaithemsua = "them";
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            
            bton_tenhtthanhtoan.Enabled = true;
            bton_luu.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            int ketqua;
            if (luoi_hinhthucthanhtoan.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            string maHTTT = grid_hinhthucthanhtoan.GetRowCellValue(grid_hinhthucthanhtoan.FocusedRowHandle, col_mahtthanhtoan).ToString();
            Mod_messagebox mess = new Mod_messagebox();
            mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            if (Variable.thongbao == true)
            {
                ketqua = f_deletethueHTTT(maHTTT);
                if (ketqua == 2)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Xóa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                // xoa mot dong trong luoi don vi tinh
                int chiso;
                chiso = ((ColumnView)luoi_hinhthucthanhtoan.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_hinhthucthanhtoan.Views[0].DataSource;
                tam.Delete(chiso);
            }

        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn thoát?", loaithongbao.yesno.GetHashCode());
            //if (Variable.thongbao == true)
            //{

                this.Close();
            //}
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            int chiso = -1;
         
            if (txt_tenhtthanhtoan.Text == "" )    // không thể luu neu chua nhap ten HTTT
                return;
            string tenHTTT = txt_tenhtthanhtoan.Text;
            Boolean hinhthuc = false;
            if (check_ththanhtoan.Checked == true)
                hinhthuc = true;
            else
                hinhthuc = false;
            int ketqua;
            if (Variable.str_trangthaithemsua == "them")
            {
                ketqua = f_insertHTTT(tenHTTT, hinhthuc);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                chiso = ((ColumnView)luoi_hinhthucthanhtoan.Views[0]).FocusedRowHandle;
                string maHTTT = grid_hinhthucthanhtoan.GetRowCellValue(grid_hinhthucthanhtoan.FocusedRowHandle, col_mahtthanhtoan).ToString();
                ketqua = f_updateHTTT(maHTTT, tenHTTT, hinhthuc);

                if (ketqua == 2)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
               
            }
            luoi_hinhthucthanhtoan.DataSource = null;
            luoi_hinhthucthanhtoan.DataSource = f_selectdmHTTT();
            if (Variable.str_trangthaithemsua == "sua" && chiso != -1)
            {
                grid_hinhthucthanhtoan.SelectRow(chiso);
                grid_hinhthucthanhtoan.FocusedRowHandle = chiso;
            }
        }

        private void grid_hinhthucthanhtoan_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_hinhthucthanhtoan.FocusedColumn;
            txt_tenhtthanhtoan.Text = grid_hinhthucthanhtoan.GetRowCellDisplayText(chiso, col_tenhtthanhtoan) + "";
            string hinhthuc = grid_hinhthucthanhtoan.GetRowCellDisplayText(chiso, col_hinhthuc) + "";
            if (hinhthuc == "Checked")
                check_ththanhtoan.Checked = true;
            else if (hinhthuc == "Unchecked")
                check_ththanhtoan.Checked = false;
          
            bton_tenhtthanhtoan.Enabled = false;
            check_ththanhtoan.Visible = true;
                
            bton_luu.Enabled = false;
        }

        private void bton_tim1_Click(object sender, EventArgs e)
        {
            //if (bton_tim1.Text == "CLR")
            //{
                //com_hinhthucno.Text = "--- Chọn tất cả ---";
                //txt_tenhtthanhtoan.Text = "";
                //bton_tim1.Text = "Tìm";
                //bton_tim1.Visible = true;
                //com_hinhthucno.Visible = true;
                //check_ththanhtoan.Visible = false;
            //}
            //else
            //{
                luoi_hinhthucthanhtoan.DataSource = null;
                luoi_hinhthucthanhtoan.DataSource = f_findHTTT();
              //  bton_tim1.Text = "CLR";
               com_hinhthucno.Visible = false;
                check_ththanhtoan.Visible = true;
                bton_tenhtthanhtoan.Enabled = true;
            //}
        }

        private void bton_tenhtthanhtoan_Click(object sender, EventArgs e)
        {
            //mh_chu frm_chu = new mh_chu();
            //frm_chu.ControlBox = false;

            //Variable.str_position_frmchu = "mhdmnhomhhc_bton_tennhomhh";
            //frm_chu.ShowDialog();
            //txt_tenhtthanhtoan.Text = Variable.str_chu;
            txt_tenhtthanhtoan.Text = new cpublic().loadformchu_point(250, 200);

        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            com_hinhthucno.Text = "--- Chọn tất cả ---";
            txt_tenhtthanhtoan.Text = "";
          // bton_tim1.Text = "Tìm";
            bton_tim1.Visible = true;
            com_hinhthucno.Visible = true;
            check_ththanhtoan.Visible = false;
        }
    }
}